package com.zjf.carpark.controller;


import com.zjf.carpark.annotation.MyLogAnno;
import com.zjf.carpark.entities.Owner;
import com.zjf.carpark.service.impl.OwnerServiceImpl;
import com.zjf.carpark.utils.ResponseData;
import com.zjf.carpark.vo.OwnerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.zjf.carpark.utils.CodeAndMessage.*;

/**
 * @description:
 * @author: 张建飞
 * @createDate: 2020/2/11
 * @version: 1.0
 */
@CrossOrigin
@Controller
@RequestMapping("owner")
public class OwnerController {

    @Autowired
    private OwnerServiceImpl ownerService;

    ResponseData responseData = new ResponseData(CODE0, ERROR);


    /**
     * 新增固定停车用户
     *
     * @param owner
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    @MyLogAnno("新增固定停车用户")
    public ResponseData add(Owner owner) {
        owner.setRegistTime(new Date());
        owner.setOwnerPwd("7cc6e4ebee3f0e7649a1c96dfff59252");
        Integer save = ownerService.save(owner);
        if (save != 0) {
            responseData = new ResponseData(CODE200, SUCCESS);
        }
        return responseData;
    }


    /**
     * 模糊查询所有
     *
     * @param ownerVO
     * @return
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public ResponseData selectAll(OwnerVO ownerVO) {
        List<Owner> owners = ownerService.selectAllByVO(ownerVO);
        int count = ownerService.countByVO(ownerVO);
        if (owners != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.setCount(count);
            responseData.putDataValue("owners", owners);
        }
        return responseData;
    }

    /**
     * 用户删除
     */
    @MyLogAnno(value = "停车用户删除")
    @RequestMapping(value = "/deleteOwner")
    @ResponseBody
    public ResponseData deleteUser(@RequestBody Map<String, Integer> data) {
        Integer ownerId = data.get("ownerId");
        int result = ownerService.deleteById(ownerId);
        if (result != 0) {
            responseData = new ResponseData(CODE200, SUCCESS);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }

    /**
     * 通过Id查询
     *
     * @return
     */
    @RequestMapping(value = "/selectById")
    @ResponseBody
    public ResponseData selectById(@RequestBody Map<String, Integer> data) {
        Integer ownerId = data.get("ownerId");
        Owner owner = ownerService.selectById(ownerId);
        if (owner != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("owner", owner);
        }
        return responseData;
    }

    /**
     * 通过Id修改
     *
     * @return
     */
    @MyLogAnno("通过ownerId修改")
    @RequestMapping(value = "/updateById")
    @ResponseBody
    public ResponseData updateById(Owner owner) {
        Integer update = ownerService.update(owner);
        if (update != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
        }
        return responseData;
    }
}
